<template>
  <el-dialog
    :model-value="showCouponDialog"
    width="900"
    :close-on-click-modal="false"
    @close="closeDialog"
  >
    <template #header>
      <p style="color: black; font-weight: bold">优惠卷选择器</p>
    </template>
    <div class="line"></div>
    <el-table
      ref="tableRef"
      :data="couponTableData"
      :loading="tableLoading"
      @selection-change="handleSelectionChange"
      :height="500"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="类型" prop="type">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.type === 1">满减卷</el-tag>
          <el-tag type="warning" v-else-if="scope.row.type === 2">折扣卷</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="优惠卷内容">
        <template #default="scope">
          {{
            scope.row.type === 1
              ? `满${scope.row.minConsumption}优惠${scope.row.discountAmount}元`
              : `满${scope.row.minConsumption}打${scope.row.discountRate}折`
          }}
        </template>
      </el-table-column>
      <el-table-column label="已领取/总数量" align="center">
        <template #default="scope">
          {{ scope.row.getCount }}/
          {{ scope.row.totalQuantity === -1 ? '无限制' : scope.row.totalQuantity }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <span v-if="scope.row.status === 0" style="color: red">禁用</span>
          <span v-else-if="scope.row.status === 1" style="color: var(--el-color-primary)"
            >启用</span
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-content" v-if="pageTotal > 10">
      <el-pagination
        v-model:current-page="form.pageNum"
        v-model:page-size="form.pageSize"
        :total="pageTotal"
        background
        layout="prev, pager, next"
        @size-change="sizeChange"
        @current-change="currentChange"
      />
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog" style="margin-right: 10px">关闭</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import { couponsList } from '@/api/marketing'

  const props = defineProps({
    showCouponDialog: {
      type: Boolean,
      default: false
    },
    defaultSelectedData: {
      type: Array
    }
  })

  const form = reactive({
    pageNum: 1,
    pageSize: 10
  })

  const tableRef = ref()
  const pageTotal = ref(0)
  const couponTableData = ref([])
  const selectedCoupons = ref<any[]>([])

  const emit = defineEmits(['closeDialog'])

  const tableLoading = ref(false)

  async function getData() {
    tableLoading.value = true
    try {
      const res = await couponsList(form)
      if (res.code === '00000') {
        couponTableData.value = res.data.list
        pageTotal.value = res.data.total

        nextTick(() => {
          selectedCoupons.value.forEach((row: any) => {
            const match = couponTableData.value.find((item: any) => item.id === row.id)
            if (match) {
              tableRef.value?.toggleRowSelection(match, true)
            }
          })
        })
      }
    } catch {
    } finally {
      tableLoading.value = false
    }
  }

  const currentChange = (val: number) => {
    form.pageNum = val
    getData()
  }
  const sizeChange = (val: number) => {
    form.pageSize = val
    form.pageNum = 1
    getData()
  }

  function handleSelectionChange(val: any[]) {
    selectedCoupons.value = val
  }

  function closeDialog() {
    emit('closeDialog')
  }

  function handleConfirm() {
    emit('closeDialog', selectedCoupons.value)
  }

  onMounted(() => {
    if (props.defaultSelectedData) {
      selectedCoupons.value = props.defaultSelectedData
    }
    getData()
  })
</script>

<style scoped lang="scss">
  .select-button {
    display: inline-block;
    padding: 6px 24px;
    border: 1px solid var(--el-color-primary);
    border-radius: 12px;
    color: var(--el-color-primary);
    background-color: #fff;
    font-size: 14px;
    cursor: pointer;
    text-align: center;
  }

  .select-button.active {
    background-color: var(--el-color-primary);
    color: white;
  }

  .line {
    height: 1px;
    width: 100%;
    background-color: #f0f0f0;
    margin-top: -20px;
    margin-bottom: 30px;
  }

  .pagination-content {
    display: flex;
    justify-content: center;

    margin-top: 20px;
  }
</style>
